<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item
                label="标题"
                prop="title"
            >
                <el-input
                    v-model="queryParams.title"
                    placeholder="请输入标题"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item
                label="关键字"
                prop="keyWord"
            >
                <el-input
                    v-model="queryParams.keyWord"
                    placeholder="请输入关键字"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item
                label="状态"
                prop="newsState"
            >
                <el-select
                    v-model="queryParams.newsState"
                    placeholder="请选择状态"
                    clearable
                    size="small"
                >
                    <el-option
                        v-for="dict in newsStateOptions"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="cyan"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                >搜索</el-button>
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                >重置</el-button>
            </el-form-item>
        </el-form>

        <el-row
            :gutter="10"
            class="mb8"
        >
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['operater:news:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['operater:news:edit']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['operater:news:remove']"
                >删除</el-button>
            </el-col>
            <right-toolbar
                :showSearch.sync="showSearch"
                @queryTable="getList"
            ></right-toolbar>
        </el-row>

        <el-table
            v-loading="loading"
            :data="newsList"
            @selection-change="handleSelectionChange"
        >
            <el-table-column
                type="selection"
                width="55"
                align="center"
            />
            <el-table-column
                label="id"
                align="center"
                prop="id"
            />
            <el-table-column
                label="标题"
                align="center"
                prop="title"
            />
            <el-table-column
                label="缩略图"
                align="center"
                width="200"
                prop="thumbnailId"
            >
                <template slot-scope="scope">
                    <img
                        :src="faceImg(scope.row.thumbnailId)"
                        alt=""
                    >
                </template>
            </el-table-column>
            <el-table-column
                label="关键字"
                align="center"
                prop="keyWord"
            />
            <el-table-column
                label="简介"
                align="center"
                prop="synopsis"
            />
            <el-table-column
                label="文章来源"
                align="center"
                prop="newsSource"
            />
            <el-table-column
                label="文章作者"
                align="center"
                prop="newsAuthor"
            />
            <el-table-column
                label="所属专科"
                align="center"
                prop="belongToSpecialty"
                :formatter="specialtyStateFormat"
            />
            <el-table-column
                label="正文"
                align="center"
                prop="mainText"
            />
            <el-table-column
                label="状态"
                align="center"
                prop="newsState"
                :formatter="newsStateFormat"
            />
            <el-table-column
                label="阅读量/浏览量"
                align="center"
                prop="viewCount"
            />
            <el-table-column
                label="发布时间"
                align="center"
                prop="createTime"
                width="180"
            >
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
            >
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                    >修改</el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-view"
                        @click="handleSeeDetail(scope.row)"
                    >查看</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

    </div>
</template>

<script>
import { listNews, getNews, delNews, addNews, updateNews, exportNews } from "@/api/operater/news";
// import UploadImage from '@/components/UploadImage';
import { baseUrl } from '@/api/config'
import Editor from '@/components/Editor';

export default {
    name: "News",
    components: {
        Editor,
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 文章信息表格数据
            newsList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 状态字典
            newsStateOptions: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                title: null,
                thumbnailId: null,
                keyWord: null,
                synopsis: null,
                newsSource: null,
                newsAuthor: null,
                belongToSpecialty: null,
                mainText: null,
                newsState: null,
                viewCount: null,
                sort: null,
            },
            depStateOptions: null,
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    computed: {
    },
    created() {
        this.getList();
        this.getDicts("belong_department").then(response => {
            this.depStateOptions = response.data;
        });
        this.getDicts("news_state").then(response => {
            this.newsStateOptions = response.data;
        });
    },
    methods: {
        faceImg(id) {
            return baseUrl(id)
        },
        /** 查询文章信息列表 */
        getList() {
            this.loading = true;
            listNews(this.queryParams).then(response => {
                this.newsList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 状态字典翻译
        newsStateFormat(row, column) {
            return this.selectDictLabel(this.newsStateOptions, row.newsState);
        },
        specialtyStateFormat(row, column) {
            return this.selectDictLabel(this.depStateOptions, row.belongToSpecialty);
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                title: null,
                thumbnailId: null,
                keyWord: null,
                synopsis: null,
                newsSource: null,
                newsAuthor: null,
                belongToSpecialty: null,
                mainText: null,
                newsState: null,
                viewCount: null,
                sort: null,
                createTime: null,
                createBy: null,
                updateTime: null,
                updateBy: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            // this.reset();
            // this.open = true;
            // this.title = "添加文章信息";
            this.$router.push('/article/addArticle')
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            this.$router.push('/article/addArticle?newsId=' + id)
        },
        handleSeeDetail(row) {
            const id = row.id || this.ids
            this.$router.push('/article/seeArticle?newsId=' + id)
        },

        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateNews(this.form).then(response => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addNews(this.form).then(res => {
                            // this.msgSuccess("新增成功");
                            if (res.code === '200') {

                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除文章信息编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delNews(ids);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有文章信息数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return exportNews(queryParams);
            }).then(response => {
                this.download(response.msg);
            })
        }
    }
};
</script>
<style lang="scss">
.app-container {
    .el-table {
        .cell {
            max-height: 100px;
            img {
                width: 100%;
            }
        }
    }
}
</style>
